package com.answeringplayer.controller;

import com.answeringplayer.Bean.Response;
import com.answeringplayer.dto.CategoryDTO;
import com.answeringplayer.dto.PracticeRecordDTO;
import com.answeringplayer.dto.QuestionBankDTO;
import com.answeringplayer.dto.QuestionDTO;
import com.answeringplayer.service.QueryService;
import com.answeringplayer.service.QuestionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/")
@RequiredArgsConstructor
public class QuestionController {

    private final QuestionService queryService;

    // 分类相关接口
    @GetMapping("categories")
    public Response<List<CategoryDTO>> getAllCategories() {
        List<CategoryDTO> categories = queryService.getAllCategories();
        return Response.success(categories);
    }

    @GetMapping("categories/{categoryId}")
    public Response<CategoryDTO> getCategoryById(@PathVariable Integer categoryId) {
        CategoryDTO category = queryService.getCategoryById(categoryId);
        if (category == null) {
            return Response.error(404, "分类不存在");
        }
        return Response.success(category);
    }

    // 题库相关接口
    @GetMapping("question-banks")
    public Response<List<QuestionBankDTO>> getAllQuestionBanks() {
        List<QuestionBankDTO> banks = queryService.getAllQuestionBanks();
        return Response.success(banks);
    }

    @GetMapping("question-banks/category/{categoryId}")
    public Response<List<QuestionBankDTO>> getQuestionBanksByCategory(@PathVariable Integer categoryId) {
        List<QuestionBankDTO> banks = queryService.getQuestionBanksByCategory(categoryId);
        return Response.success(banks);
    }

    @GetMapping("question-banks/{bankId}")
    public Response<QuestionBankDTO> getQuestionBankDetail(@PathVariable Integer bankId) {
        QuestionBankDTO bankDetail = queryService.getQuestionBankDetail(bankId);
        if (bankDetail == null) {
            return Response.error(404, "题库不存在");
        }
        return Response.success(bankDetail);
    }

    @GetMapping("question-banks/page")
    public Response<List<QuestionBankDTO>> getQuestionBanksByPage(
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        List<QuestionBankDTO> banks = queryService.getQuestionBanksByPage(categoryId, keyword, page, pageSize);
        return Response.success(banks);
    }

    // 题目相关接口
    @GetMapping("questions/bank/{bankId}")
    public Response<List<QuestionDTO>> getQuestionsByBank(@PathVariable Integer bankId) {
        List<QuestionDTO> questions = queryService.getQuestionsByBank(bankId);
        return Response.success(questions);
    }

    @GetMapping("questions/{questionId}")
    public Response<QuestionDTO> getQuestionDetail(@PathVariable Long questionId) {
        QuestionDTO questionDetail = queryService.getQuestionDetail(questionId);
        if (questionDetail == null) {
            return Response.error(404, "题目不存在");
        }
        return Response.success(questionDetail);
    }

    @GetMapping("questions/page")
    public Response<List<QuestionDTO>> getQuestionsByPage(
            @RequestParam(required = false) Integer bankId,
            @RequestParam(required = false) String questionType,
            @RequestParam(required = false) Integer difficulty,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer pageSize) {
        List<QuestionDTO> questions = queryService.getQuestionsByPage(bankId, questionType, difficulty, page, pageSize);
        return Response.success(questions);
    }


}